<?php

namespace app\admin\controller\kupay;

use app\common\controller\Backend;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
/**
 * 发送记录
 *
 * @icon fa fa-circle-o
 */
class Setlog extends Backend
{

    /**
     * Setlog模型对象
     * @var \app\admin\model\kupay\Setlog
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\kupay\Setlog;
        $company = Db::name('kupay_compay')->find();
        $this->assign('company', $company);
        if (empty($company) || empty($company['company'])) {
            $this->assignconfig('companyinfo', 1);
        } else {
            $this->assignconfig('companyinfo', 2);
        }
        header('Cache-control: private, must-revalidate');

    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index($year1 = '', $year2 = 0)
    {
        $date = '';
        if (empty($year1)) {
            $year1 = date("Y", time());
        }
        $map = [];
        if (empty($year2)) {
            $map['times'] = ['like', "%{$year1}%"];
        } else {
            $map['times'] = $year1 . '-' . $year2;

        }

        $list = Db::name('kupay_setlog')->where($map)->order('createtime desc')->order('times desc')
            ->paginate(12, false, [ 'query' => request()->param()])->each(function($item, $key){
            $lists = Db::name('kupay_paylog')->where('pid', $item['id'])->order('updatetime desc')->select();
            $b = array();
            foreach ($lists as $vs) {
                $b[] = $vs['type'];
            }
            $c = array_unique($b);
            $c = count($c);
            $item['number'] = count($lists) / $c;
            $item['newtimes'] = date('Y-m-d H:i:s', $lists[0]['updatetime']);
            $times = explode('-', $item['times']);
            $item['times1'] = $times[0];
            $item['times2'] = $times[1];
            return $item;
        });
        $this->assign('list', $list);
        $this->assign('year1', $year1);
        $this->assign('year2', $year2);
        return $this->view->fetch();

    }

    /**
     * 查看
     */
    public function info($id, $page = 1, $type = 0, $name = '')
    {
        $map = [];
        if (!empty($name)) {
            $map['name'] = ['like', "%{$name}%"];
        }
        $setlog = Db::name('kupay_setlog')->where('id', $id)->find();

        $list = [];
        $list1 = Db::name('kupay_paylog')
            ->where('pid', $id)
            ->where('type', 1)
            ->column('id');

        $list4 = Db::name('kupay_paylog')
            ->where('pid', $id)
            ->where('type', 4)
            ->column('id');
        if (empty($type)) {
            if (!empty($list1)) {
                $type = 1;
            } else if (!empty($list4)) {
                $type = 4;
            }
            $list = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where($map)
                ->paginate(10, false, ['page' => $page, 'query' => request()->param()]);
            $count = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where($map)->count();
            $count1 = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where('status', 0)
                ->where($map)->count();
            $count2 = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where('status', 1)
                ->where($map)->count();
            $count3 = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where('status', 2)
                ->where($map)->count();
            $this->assign('count', $count);
            $this->assign('count1', $count1);
            $this->assign('count2', $count2);
            $this->assign('count3', $count3);
            $this->assignconfig('count', $count);
            $this->assignconfig('count1', $count1);
            $this->assignconfig('count2', $count2);
            $this->assignconfig('count3', $count3);
        } else {
            $list = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where($map)
                ->paginate(10, false, ['page' => $page, 'query' => request()->param()]);
            $count = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where($map)->count();
            $count1 = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where('status', 0)
                ->where($map)->count();
            $count2 = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where('status', 1)
                ->where($map)->count();
            $count3 = Db::name('kupay_paylog')
                ->where('pid', $id)
                ->where('type', $type)
                ->where('status', 2)
                ->where($map)->count();
            $this->assign('count', $count);
            $this->assign('count1', $count1);
            $this->assign('count2', $count2);
            $this->assign('count3', $count3);
            $this->assignconfig('count', $count);
            $this->assignconfig('count1', $count1);
            $this->assignconfig('count2', $count2);
            $this->assignconfig('count3', $count3);
        }
        $times = explode('-', $setlog['times']);
        $years = $times[0] . '年' . $times[1] . '月';

        if (!empty($setlog['timing'])) {
            $this->assign('name', $name);
            $this->assign('years', $years);
            $this->assign('setlog', $setlog);
            $this->assign('list', '');
            $this->assign('type', $type);
            $this->assign('id', $id);
            return $this->view->fetch();
        }

        $this->assign('name', $name);
        $this->assign('years', $years);
        $this->assign('setlog', $setlog);
        $this->assign('list', $list);
        $this->assign('type', $type);
        $this->assign('id', $id);
        return $this->view->fetch();
    }

    public function exports($id,$type)
    {
        $map = [];
        $paylog = Db::name('kupay_paylog')->where('pid', $id)->where('type', $type)->order('status desc')->select();
        if (empty($paylog)) {
            $this->error('无可导出数据');
        }
        $data = [];
        foreach ($paylog as $key => $value) {
            $arr = [];
            switch ($value['status']) {
                case 1:
                    $status = '成功';
                    break;
                case 2:
                    $status = '撤回';
                    break;
                default:
                    $status = '失败';
                    break;
            }
            $arr['sort'] = $key + 1;
            $arr['name'] = $value['name'];
            $arr['phone'] = $value['phone'];
            $arr['times'] =  date("Y-m-d h:i:s", $value['updatetime']);
            $arr['status'] = $status;
            $arr['info'] = $value['errs'];
            $data[] = $arr;
        }
        $title = array('序号', '姓名', '手机', '发送时间', '发送状态', '失败原因');//表头数组
        // Create new Spreadsheet object
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        //表头
        //设置单元格内容
        $titCol = 'A';
        foreach ($title as $key => $value) {
            // 单元格内容写入
            $sheet->setCellValue($titCol . '1', $value);
            $spreadsheet->getActiveSheet()->getColumnDimension($titCol)->setWidth(30);
            $titCol++;
        }
        $row = 2; // 从第二行开始
        foreach ($data as $item) {
            $dataCol = 'A';
            // 单元格内容写入
            $sheet->setCellValue($dataCol . $row, $item['sort']);
            $dataCol++;
            $sheet->setCellValue($dataCol . $row, $item['name']);
            $dataCol++;
            $sheet->setCellValue($dataCol . $row, $item['phone']);
            $dataCol++;
            $sheet->setCellValue($dataCol . $row, $item['times']);
            $dataCol++;
            $sheet->setCellValue($dataCol . $row, $item['status']);
            $dataCol++;
            $sheet->setCellValue($dataCol . $row, $item['info']);
            $dataCol++;
            $row++;
        }

        $name = '工资表';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //删除清空：
        $spreadsheet->disconnectWorksheets();

    }
    /**
     * 查看
     */
    public function infodetails($id)
    {
        $list = Db::name('kupay_paylog')
            ->where('id', $id)
            ->find();
        $listinfo = Db::name('kupay_paylog_auth')
            ->alias('a')
            ->join('kupay_paylog_rule w', 'a.paylog_rule_id = w.id')
            ->where('paylog_id', $id)->select();
        $setlog = Db::name('kupay_setlog')->where('id', $list['pid'])->find();
        $times = explode('-', $setlog['times']);
        $years1 = $times[0] . '年';
        $years2 = $times[1] . '月';
        $this->assign('years1', $years1);
        $this->assign('years2', $years2);
        $this->assign('setlog', $setlog);
        $this->assign('list', $list);
        $this->assign('listinfo', $listinfo);
        return $this->view->fetch();
    }

    /**
     * 修改
     */
    public function infoedit($id)
    {
        $list = Db::name('kupay_paylog')
            ->where('id', $id)
            ->find();
        $listinfo = Db::name('kupay_paylog_auth')
            ->alias('a')
            ->join('kupay_paylog_rule w', 'a.paylog_rule_id = w.id')
            ->where('paylog_id', $id)->select();
        $setlog = Db::name('kupay_setlog')->where('id', $list['pid'])->find();
        $times = explode('-', $setlog['times']);
        $years1 = $times[0] . '年';
        $years2 = $times[1] . '月';
        $this->assign('years1', $years1);
        $this->assign('years2', $years2);
        $this->assign('setlog', $setlog);
        $this->assign('list', $list);
        $this->assign('listinfo', $listinfo);
        return $this->view->fetch();
    }

//    保存

    public function infosave()
    {
        $input = input();

        $id = $input['id'];
        $price = $input['price'];
        unset($input['price']);
        unset($input['id']);
        $data =
        $stat = 1;
        // 启动事务
        Db::startTrans();
        try {
            $data = ['price' => $price];
            foreach ($input as $k => $v) {
                $ks = explode('_', $k);
                $datas = ['content' => $v];
                $auths= Db::name('kupay_paylog_auth')->where('paylog_id', $id)->where('paylog_rule_id', $ks['1'])->find();
                $auths= Db::name('kupay_paylog_rule')->where('id', $auths['paylog_rule_id'])->find();
                if(strstr($auths['name'],'手机')){
                    $data['phone']=$v;
                }
                if(strstr($auths['name'],'邮箱')){
                    $data['email']=$v;
                }
                Db::name('kupay_paylog_auth')->where('paylog_id', $id)->where('paylog_rule_id', $ks['1'])->update($datas);
            }
            Db::name('kupay_paylog')->where('id', $id)->update($data);
            $stat = 2;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if ($stat == 2) {
            $this->success('修改成功！');
        } else {
            $this->error('修改失败！');
        }

    }

}
